package ucuserslogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
)

type QueryActivityConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryActivityConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryActivityConfigLogic {
	return &QueryActivityConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建活动设置
func (l *QueryActivityConfigLogic) QueryActivityConfig(in *pb.QueryActivityConfigReq) (*pb.QueryActivityConfigResp, error) {
	defer utils.RecoverPanic()

	resp, err := l.svcCtx.ActivityConfigModel.Find(l.ctx, in.PageNo, in.PageSize)
	if err != nil {
		l.Logger.Errorf("QueryActivityConfigReq Find failed ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	r := &pb.QueryActivityConfigResp{
		List: make([]*pb.QueryActivityData, 0),
	}
	for _, d := range resp {
		queryData := &pb.QueryActivityData{
			Id:            d.Id,            // 序号
			Title:         d.Title,         // 活动标题
			Content:       d.Content,       // 活动内容
			Object:        d.Object,        // 对象
			ClickWay:      d.Clickway,      // 点击方式 0--领取奖励 1--跳转
			Frequency:     d.Frequency,     // 频率 0--总数 1--每天
			Times:         d.Times,         // 次数
			NumPerOneTime: d.NumPerOneTime, // 数量/每次
			JumpToPage:    d.JumpToPage,    // 跳转页面
			Status:        d.Status,        // 状态
			Operate:       d.Operate,
		}
		r.List = append(r.List, queryData)
	}
	r.Total, err = l.svcCtx.ActivityConfigModel.Count(l.ctx)
	if err != nil {
		l.Logger.Errorf("QueryActivityConfigReq Count failed ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return r, nil
}
